﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.NLogSdk;
using ThresholdAssistant.WebAPI.Models;

namespace ThresholdAssistant.WebAPI.Repository
{

    /// <summary>
    /// 短信发送记录记录表仓储
    /// </summary>
    public class SendSmsRecordRepository : BaseRepository, IBaseAutofac
    {
        /// <summary>
        /// 新增
        /// </summary>
        public async Task<bool> AddAsync(SendSmsRecord model)
        {
            string insertSql = @"insert into `t_send_sms_record`
                                    (`type`,`source`,`phone`,`sms_id`,`template_id`,`status`,`message`,`create_time`,`update_time`,`remark`)
                                values
                                    (@Type,@Source,@Phone,@SmsId,@TemplateId,@Status,@Message,@CreateTime,@UpdateTime,@Remark)";
            return await mydb.ExecuteAsync(insertSql, model);
        }

        /// <summary>
        /// 新增
        /// </summary>
        public async Task<bool> AddAsync(IEnumerable<SendSmsRecord> list)
        {
            try
            {
                string insertSql = @"insert into `t_send_sms_record`
                                    (`type`,`source`,`phone`,`sms_id`,`template_id`,`status`,`message`,`create_time`,`update_time`,`remark`)
                                values
                                    (@Type,@Source,@Phone,@SmsId,@TemplateId,@Status,@Message,@CreateTime,@UpdateTime,@Remark)";
                return await mydb.ExecuteAsync(insertSql, list);
            }
            catch (System.Exception ex)
            {
                NLogUtil.Error($"批量新增短信发送记录异常:list:{JsonConvert.SerializeObject(list)},ex:{ex}");
                return false;
            }
        }

        /// <summary>
        /// 获取短信发送统计数据
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<IEnumerable<SmsStatistics>> GetStatisticsAsync(DateTime startTime, DateTime endTime)
        {

            var selectSql = @"select
	                        	date_format( create_time, '%Y-%m-%d' ) as send_date,
	                            type,
	                            count( 1 )  as num_count,
	                            sum(case when `status` = 1 then 1 else 0 end) as success_count,
	                            sum(case when `status` = 2 then 1 else 0 end) as fail_count
                            from
	                            t_send_sms_record
                            where create_time >= @StartTime and create_time <= @EndTime
                            group by date_format(create_time,'%Y-%m-%d'),type
                            order by date_format(create_time,'%Y-%m-%d') desc";

            return await mydb.GetListAsync<SmsStatistics>(selectSql, new
            {
                StartTime = startTime,
                EndTime = endTime
            });
        }
    }
}
